/*

BlackRabbit - OpenSource IDE.
@author Edward Sarkisyan (edward.sarkisyan@gmail.com)
Use LGPL (http://www.gnu.org/copyleft/lesser.html)

*/

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialogs/newprojectdialog.h"
#include "dialogs/programsettings.h"
#include <QFileSystemModel>
#include <QFileDialog>
#include <QXmlStreamReader>
#include <QMessageBox>
#include <QPluginLoader>
#include "dialogs/pluginsmanagedialog.h"
#include "dialogs/laguagemanager.h"
#include "widgets/editor.h"
#include "widgets/sourceeditor.h"
#include "plugininterfaces.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //notificationArea = ui->notifyArea;
    ui->menuBar->hide();
    ui->filesystemTreeView->setModel(&dirModel);
    // ? //ui->projectTreeWidget->setTabsContainer(ui->openedFilesTabWidget);
    mainMenu = new QMenu(this);
    setMenus();
    //connect(ui->action_Open_file, SIGNAL(triggered()), this, SLOT(on_action_Open_file_triggered()));
    // Double clicking for opening project file
    connect(ui->projectTreeWidget->treeWidget(), SIGNAL(doubleClicked(QModelIndex)), this, SLOT(openFile(QModelIndex)) );
}

void MainWindow::setMenus()
{
    /*ui->fileMenuBttn->setMenu(ui->menu_File);
    ui->editMenuBttn->setMenu(ui->menu_Edit);
    ui->projectMenuBttn->setMenu(ui->menu_Project);
    ui->buildAndDebugMenuBttn->setMenu(ui->menu_Build_and_Debug);
    ui->toolsMenuBttn->setMenu(ui->menu_Tools);
    ui->helpMenuBttn->setMenu(ui->menu_Help);*/
    mainMenu->addMenu(ui->menu_File);
    mainMenu->addMenu(ui->menu_Edit);
    mainMenu->addMenu(ui->menu_Project);
    mainMenu->addMenu(ui->menu_Build_and_Debug);
    mainMenu->addMenu(ui->menu_Tools);
    mainMenu->addMenu(ui->menu_Help);
    ui->buildButton->setMenu(mainMenu);
}

MainWindow::~MainWindow()
{
    delete mainMenu;
    delete ui;
}

void MainWindow::on_action_New_Project_triggered()
{
    NewProjectDialog newProjectDialog(this);
    newProjectDialog.exec();
    on_action_Open_Project_triggered(newProjectDialog.getProjectFileName());
}

void MainWindow::on_action_Open_Project_triggered(QString projectFileName)
{
    if(projectFileName.isEmpty())
        projectFileName = QFileDialog::getOpenFileName(this,
                                                    tr("Select project file"),
                                                    "");
    ui->projectTreeWidget->setProjectFile(projectFileName);
}

void MainWindow::on_action_Plugins_triggered()
{
    PluginsManageDialog *pMD = new PluginsManageDialog(this, MANAGE);
    pMD->exec();
}

void MainWindow::addPluginWidget()
{
}

void MainWindow::on_leftPluginAddBttn_clicked()
{
    //PluginsManageDialog *pMD = new PluginsManageDialog(this, ADD);
   // pMD->exec();
    //delete ui->leftBottomWidget;
    //pMD->getPlugin()->preview(ui->leftBottomWidget);
}

void MainWindow::on_action_New_file_triggered()
{
}

void MainWindow::on_action_Settings_triggered()
{
    ProgramSettings* mainSettings = new ProgramSettings(this);
    mainSettings->exec();
    delete mainSettings;
}

void MainWindow::on_action_Open_file_triggered()
{
    QString openFileFilters;
    /*
      QString openFileFilters = getOpenFileFilters();
      */
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open file"),QString(),
                                 tr(openFileFilters.toStdString().c_str()));
      openFile(fileName);
}


void MainWindow::openFile(QString fileName)
{
    if(!fileName.isNull())
    {
        /*
          emitOpening file
          findAndGetExtentioon
          creatingEditor
          opening
          */
        qDebug("MainWindow::openFile(QString fileName) : trying to open file");
        QFile openedF(fileName);
        if(!openedF.open(QIODevice::ReadWrite))
        {
            QMessageBox::critical(this,tr("Can't open file"),tr("Errow emitted in opening file"));
            return;
        }
        Editor *edit = new Editor(ui->openedFilesTabWidget);
        edit->setText(openedF.readAll());
        ui->openedFilesTabWidget->addTab(edit, QFileInfo(fileName).fileName());
        ui->openedFilesTabWidget->setCurrentWidget(edit);
    }
    else
    {
        QMessageBox::warning(this,
                             tr("File can't be opened"),
                             tr("File name is empty, wrong path!"),
                             QMessageBox::Ok,
                             QMessageBox::Ok);
    }
}

void MainWindow::on_buildButton_clicked()
{
    ui->buildButton->showMenu();
}

void MainWindow::on_addPluginToBarevTabBttn_clicked()
{
    ui->addPluginToBarevTabBttn->setShown(false);
    PluginsManageDialog *pMD = new PluginsManageDialog(this, ADD);
    pMD->exec();
    Plugin* addingPlugin = pMD->getPlugin();
    if( addingPlugin != 0)
    {
        ui->barevPluginLayout->insertWidget(0, addingPlugin->getPreviewWidget());
        //if(pMD->getPlugin()->preview() != 0)
        {
            connect(addingPlugin->preview(),
                    SIGNAL(notify(NOTIFICATION_INFORMATION)),
                    ui->notifyArea,
                    SLOT(notify(NOTIFICATION_INFORMATION))
                    );
        }
    }
}

void MainWindow::on_openedFilesTabWidget_tabCloseRequested(int index)
{
    if(index == 0)
        return;
    (dynamic_cast<Editor*>(ui->openedFilesTabWidget->widget(index)))->maybeSave();
    ui->openedFilesTabWidget->removeTab(index);
}

void MainWindow::on_actionExtentions_triggered()
{
    LaguageManager *lM = new LaguageManager(this);
    lM->exec();
}

void MainWindow::on_rabbitMenuBttn_clicked()
{
    RabbitMenu *rM = new RabbitMenu(this);
    rM->move(ui->rabbitMenuBttn->pos());
    rM->show();
}
